<?php

Yii::import('zii.widgets.grid.CDataColumn');

class CGroupColumn extends CDataColumn
{
	private
	/**
	 * @var string 
	 */
	public $groupName;


	/**
	 * Initializes the column.
	 */
	public function init()
	{
		parent::init();
		if($this->groupName===null)
			throw new CException(Yii::t('zii','"groupName" must be specified for CGroupColumn.'));
		$groups = array();
				$groupData=array();
				for($row=0;$row<$n;++$row)
				{		
					$groupId=CHtml::value($data[$row],$this->groupName);								
					if(!isset($groups[$groupId]))
						$groups[$groupId]=array();
					$groups[$groupId][]=$row;
				}
				foreach($groups as $group=>$rows)
				{
					$this->renderGroupHeader($group);
					echo "<tbody>\n";
					foreach($rows as $row)
						$this->renderTableRow($row);
					echo "</tbody>\n";
					$this->renderGroupFooter($rows);
				}				
	}

	/**
	 * Renders the footer cell content.
	 * The default implementation simply renders {@link footer}.
	 * This method may be overridden to customize the rendering of the footer cell.
	 */
	protected function renderFooterCellContent()
	{
		echo trim($this->footer)!=='' ? $this->footer : '&nbsp;';
	}
	
	/**
	 * Renders the data cell content.
	 * This method evaluates {@link value} or {@link name} and renders the result.
	 * @param integer $row the row number (zero-based)
	 * @param mixed $data the data associated with the row
	 */
	protected function renderDataCellContent($row,$data)
	{
		if($this->value!==null)
			$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
		else if($this->name!==null)
			$value=CHtml::value($data,$this->name);
		echo $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
	}
}
